
import { Component, OnInit, ViewChild } from '@angular/core';
import 'rxjs/add/operator/map';
import { CommService } from '../../comm/comm.service';
import { ReportUrls } from '../../comm/report.urls.conf';
import { SubArea } from '../../model/report/sub-area';
import { QueryModel } from '../../model/report/query-model';
declare const $: any;
import { DxDropDownBoxComponent } from 'devextreme-angular';

@Component({
    selector: 'app-config-moee',
    templateUrl: './config-moee.component.html',
    styleUrls: ['./config-moee.component.scss']
})
export class ConfigMoeeComponent implements OnInit {
    title = '月度设备有效利用率配置';
    public templateItems: any[] = [];
    public templateSel;
    /*生产线 - 班次*/
    public subAreaItems: SubArea[];
    public subAreaSelected: any;
    public equipemntItems: any[] = [];
    public equipmentSelected: any;

    public shiftItems: any[];
    public shiftSelected: any;
    public templateDetail = [];
    public moeeList = [];
    public newItem = {
        workday: '',
        subArea: '',
        templateName: '',
        h1: 0, h2: 0, h3: 0, h4: 0, h5: 0, h6: 0, h7: 0, h8: 0, h9: 0, h10: 0, h11: 0,
        subValue: 0, changeDesc: '', queryStation: '', shift: 0
    };
    tempCount = 0;

    defaultTmp;

    @ViewChild('dddb') dddb: DxDropDownBoxComponent;
    @ViewChild('ddda') ddda: DxDropDownBoxComponent;

    constructor(private commServices: CommService) {
    }

    ngOnInit() {
        this.commServices.initDatePicker2($('#newTime'), 'YYYY-MM-DD',true);
        this.commServices.initDatePicker($('#DateTimeRange'), 'YYYY-MM-DD');
        this.commServices.initDatePicker($('#newTimeRange'), 'YYYY-MM-DD');

        this.shiftItems = [
            { shiftId: 1, shiftName: '早班' },
            { shiftId: 2, shiftName: '中班' },
            { shiftId: 3, shiftName: '晚班' },
        ];
        this.shiftSelected = this.shiftItems[0];
        this.subAreaItems = this.commServices.getSubAreas();
        this.subAreaSelected = this.subAreaItems[0];

        this.equipemntItems = this.commServices.getTVResourceBySubArea(this.subAreaSelected.subArea);
        this.equipmentSelected = this.equipemntItems[0];
        this.initTemplate();
        this.getDefaultTmp();

    }
    calcValue() {
        this.newItem.subValue = (this.newItem.h1 + this.newItem.h2 + this.newItem.h3 + this.newItem.h4
            + this.newItem.h5 + this.newItem.h6 + this.newItem.h7 + this.newItem.h8
            + this.newItem.h9 + this.newItem.h10 + this.newItem.h11) - this.tempCount;
    }

    clearEquipmentSelected() {
    }
    equipmentSelectedOk() {
        this.dddb.instance.close();
    }
    clearSubAreaSelected() {
        this.subAreaSelected = [];
    }
    subAreaSelectedOk() {
        this.ddda.instance.close();
    }

    initTemplate() {
        this.templateItems = [];
        this.templateSel = {};
        this.commServices
            .doGet(ReportUrls.ProdHourTemplateAreas2 + '/' + this.subAreaSelected.subArea)
            .subscribe(model => {
                if (model.resultCode === 200) {
                    this.templateItems = model.data;
                    this.templateSel = this.templateItems.find(item=>item.templateName === this.defaultTmp.defaultTmp);
                } else {
                    alert(model.resultMessage);
                }
            });
    }

    onSubAreaSelectedChange(subArea: any): void {
        this.equipemntItems = [];
        this.equipemntItems = this.commServices.getTVResourceBySubArea(subArea.value.subArea);

       
        this.subAreaSelected = subArea.value;
        
        // 获取此区域的默认模板
        this.getDefaultTmp()

    }

    getDefaultTmp(){
        this.commServices
        .doGet( '/moee/default/' + this.subAreaSelected.subArea)
        .subscribe(model => {
            if (model.resultCode === 200) {
                this.defaultTmp = model.data;
                this.initTemplate();

            }
        })
    }

    onShiftSelectedChange(shi: any) {
        this.onTemplateSelectedChange();
    }

    onTemplateSelectedChange() {
        this.commServices
            .doGet(ReportUrls.ProdHourTemplateDetail + '/' + this.templateSel.templateName)
            .subscribe(model => {
                if (model.resultCode === 200) {
                    this.templateDetail =  model.data;
                    this.tempCount = 0;
                    this.newItem.shift = this.shiftSelected.shiftId;
                    model.data = model.data.filter(e => e.shiftId === this.newItem.shift);
                    this.newItem.h1 = model.data.filter(e => e.hour === 1)[0].jph;
                    this.newItem.h2 = model.data.filter(e => e.hour === 2)[0].jph;
                    this.newItem.h3 = model.data.filter(e => e.hour === 3)[0].jph;
                    this.newItem.h4 = model.data.filter(e => e.hour === 4)[0].jph;
                    this.newItem.h5 = model.data.filter(e => e.hour === 5)[0].jph;
                    this.newItem.h6 = model.data.filter(e => e.hour === 6)[0].jph;
                    this.newItem.h7 = model.data.filter(e => e.hour === 7)[0].jph;
                    this.newItem.h8 = model.data.filter(e => e.hour === 8)[0].jph;
                    this.newItem.h9 = model.data.filter(e => e.hour === 9)[0].jph;
                    this.newItem.h10 = model.data.filter(e => e.hour === 10)[0].jph;
                    this.newItem.h11 = model.data.filter(e => e.hour === 11)[0].jph;
                    this.tempCount =
                        this.newItem.h1 + this.newItem.h2 + this.newItem.h3 + this.newItem.h4
                        + this.newItem.h5 + this.newItem.h6 + this.newItem.h7 + this.newItem.h8
                        + this.newItem.h9 + this.newItem.h10 + this.newItem.h11;
                    this.newItem.subValue = 0;
                } else {
                    alert(model.resultMessage);
                }
            });
    }

    saveConfig() {
        this.newItem.workday = this.getTime('newTime');
        this.newItem.templateName = this.templateSel.templateName;
        this.newItem.subArea = this.subAreaSelected.subArea;
        this.newItem.queryStation = this.equipmentSelected.stationCode;
        this.newItem.shift = this.shiftSelected.shiftId;
        this.commServices
            .getList(this.newItem, '/moee/new')
            .subscribe(model => {
                if (model.resultCode === 200) {
                    alert('操作成功！');
                }
            });
    }

    saveMoee(){

        const model = new QueryModel();

        model.dateTimeRange = this.getTime('newTimeRange');
        model.tag = this.templateSel.templateName;
        model.querySubAreaIds = [this.subAreaSelected.subArea];
        model.queryResourceIds = [this.equipmentSelected.stationCode];

        this.commServices
            .getList(model, '/moee/new/range')
            .subscribe(model => {
                if (model.resultCode === 200) {
                    alert('操作成功！');
                }
            });
    }

    saveChanges() {
        this.commServices
            .getList(this.moeeList, '/moee/upItem')
            .subscribe(model => {
                if (model.resultCode === 200) {
                    alert('操作成功！');
                }
            });
    }

    changeDefault(template){
        if(confirm('确认修改？')){
            this.commServices
            .doGet( `/moee/change/${this.subAreaSelected.subArea}/${template.templateName}`)
            .subscribe(model => {
                if (model.resultCode === 200) {
                    alert('修改成功')
                    this.getDefaultTmp();
                    
                }
            })
        }
    }



delData(itemData: any) {
    debugger
        if (confirm('是否删除')) {
            this.commServices.doPost('/moee/delItem',itemData).subscribe(model => {
                if (model.resultCode === 200) {
                     this.getReportData();
                    // this.getReportDataP(this.curItem);
                } else {
                    // console.log(model.resultCode + ',' + model.resultMessage);
                    //alert('下载失败！' + model.resultMessage);
                }
            });
        }
    }


    selChange(e) {
        if (e.data.queryStation) {
            this.commServices
                .getList({
                    subArea: this.subAreaSelected.subArea,
                    templateName: e.data.templateName,
                    shift: this.shiftSelected.shiftId,
                }, '/moee/upStation')
                .subscribe(model => {
                    if (model.resultCode === 200) {
                        this.getReportData();
                    }
                });
        }
        if (e.data.defaultTmp) {
            this.commServices
                .getList({
                    subArea: this.subAreaSelected.subArea,
                    defaultTmp: e.data.defaultTmp,
                }, '/moee/upTmp')
                .subscribe(model => {
                    if (model.resultCode === 200) {
                        this.getReportData();
                    }
                });
        }

    }

    getReportData(): void {
        const queryModel: QueryModel = new QueryModel();
        queryModel.dateTimeRange = this.getTime('DateTimeRange');
        queryModel.querySubAreaIds = [this.subAreaSelected.subArea];
        this.commServices
            .getList(queryModel, '/moee/list')
            .subscribe(model => {
                if (model.resultCode === 200) {
                    this.moeeList = model.data;
                    this.equipmentSelected = 
                    this.equipemntItems.find(item=>item.stationCode === this.moeeList[0].queryStation);
                    this.getDefaultTmp();
                } else {
                    alert('查询错误：' + model.resultMessage);
                }
            });
    }
    

    getTime(id: any): string {
        return $('#' + id).val();
    }

}
